package com.activities;


import java.util.List;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.imgproc.Imgproc;
import com.camera.CamPreview;
import com.detection.Hand;
import com.detection.HandDetect;
import com.example.magic.R;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;

public class DetectTestActivity extends Activity implements OnTouchListener, CvCameraViewListener2 {
	private static final String  TAG              = "Calibration Activity";

	private Mat                  mRgba;
	private Scalar centerColorHsv;

	private Size                 SPECTRUM_SIZE;
	private CameraBridgeViewBase mOpenCvCameraView;

	private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
		@Override
		public void onManagerConnected(int status) {
			switch (status) {
			case LoaderCallbackInterface.SUCCESS:
			{
				Log.i(TAG, "OpenCV loaded successfully");
				System.loadLibrary("magic");

				HandDetect.init(DetectTestActivity.this);

				mOpenCvCameraView.enableView();
				mOpenCvCameraView.setOnTouchListener(DetectTestActivity.this);
			} break;
			default:
			{
				super.onManagerConnected(status);
			} break;
			}
		}
	};
private CamPreview camPreview;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		setContentView(R.layout.detect_test_view);

		mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.full_screen_view);
		mOpenCvCameraView.setCvCameraViewListener(this);

	}

	@Override
	public void onPause()
	{
		super.onPause();
		if (mOpenCvCameraView != null)
			mOpenCvCameraView.disableView();
	}

	@Override
	public void onResume()
	{
		super.onResume();
		OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
	}

	public void onDestroy() {
		super.onDestroy();
		if (mOpenCvCameraView != null)
			mOpenCvCameraView.disableView();
	}

	public void onCameraViewStarted(int width, int height) {
		mRgba = new Mat(height, width, CvType.CV_8UC4);
		SPECTRUM_SIZE = new Size(200, 64);
	}

	public void onCameraViewStopped() {
		mRgba.release();
	}

	public boolean onTouch(View v, MotionEvent event) {
		int cols = mRgba.cols();
		int rows = mRgba.rows();

    	Intent intent = new Intent(this, CombinedTestActivity.class);
        startActivity(intent);    

		return false; // don't need subsequent touch events
	}

	public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
		mRgba = inputFrame.rgba();

		List<Hand> hands = HandDetect.findHands(inputFrame);
		Log.d("hand count", ""+hands.size());
		for(Hand hand:hands){
			Rect rect = hand.getRect();
			Log.d("hand", rect.toString());
			Core.rectangle(mRgba, rect.tl(), rect.br(), hand.isOpen()?new Scalar(255, 255, 0):new Scalar(0, 255, 255), 2, 8, 0 );
		}

		return mRgba;
	}

	@SuppressWarnings("unused")
	private Scalar convertScalarHsv2Rgba(Scalar hsvColor) {
		Mat pointMatRgba = new Mat();
		Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
		Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);

		return new Scalar(pointMatRgba.get(0, 0));
	}
}
